﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Text;

namespace EasyNet
{
    static public class TCPTools
    {
        /// <summary>
        /// 检查客户端套接字是否处于连接状态的方法
        /// </summary>
        /// <param name="socket_"></param>
        /// <returns></returns>
        static public bool SocketConnected(Socket socket_)
        {
            bool part1 = socket_.Poll(1000, SelectMode.SelectRead);
            bool part2 = (socket_.Available == 0);
            if (part1 && part2)
                return false;
            else
                return true;
        }
        static public Socket? EasyConnect(string realm_name_or_ip, string port_)
        {
            return EasyConnect(realm_name_or_ip, Convert.ToInt32(port_));
        }
        static public Socket? EasyConnect(string realm_name_or_ip, int port_)
        {
            Socket clientSocket;
            IPEndPoint serverEndPoint;
            //根据IP地址或域名参数获取远程服务器IP地址
            IPAddress serverIP = Dns.GetHostAddresses(realm_name_or_ip)[0];
            switch (serverIP.AddressFamily)
            {
                case AddressFamily.InterNetwork:
                    clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    break;
                case AddressFamily.InterNetworkV6:
                    clientSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                    break;
                default:
                    return null;
            }
            serverEndPoint = new IPEndPoint(serverIP, port_);//将此地址绑定到远程服务器终结点
            clientSocket.Connect(serverEndPoint);//请求连接远程服务器
            return clientSocket;
        }
        static public Socket? BeginEasyConnect(string realm_name_or_ip, string port_, AsyncCallback callback_)
        {
            return BeginEasyConnect(realm_name_or_ip, Convert.ToInt32(port_), callback_);
        }
        static public Socket? BeginEasyConnect(string realm_name_or_ip, int port_, AsyncCallback callback_)
        {
            Socket clientSocket;
            IPEndPoint serverEndPoint;
            //根据IP地址或域名参数获取远程服务器IP地址
            IPAddress serverIP = Dns.GetHostAddresses(realm_name_or_ip)[0];
            switch (serverIP.AddressFamily)
            {
                case AddressFamily.InterNetwork:
                    clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    break;
                case AddressFamily.InterNetworkV6:
                    clientSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                    break;
                default:
                    return null;
            }
            serverEndPoint = new IPEndPoint(serverIP, port_);//将此地址绑定到远程服务器终结点
            clientSocket.BeginConnect(serverEndPoint, callback_, clientSocket);//请求连接远程服务器
            return clientSocket;
        }
    }
}
